package main

import (
	"context"
	"net/http"
	"time"

	"gitee.com/youkelike/ziam/gshutdown"
	"gitee.com/youkelike/zlog"
	"github.com/gin-gonic/gin"
)

func main() {
	router := gin.Default()
	router.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "success.")
	})

	srv := &http.Server{
		Addr:    ":9000",
		Handler: router,
	}

	gs := gshutdown.New()
	gs.AddShutdownCallback(gshutdown.ShutdownFunc(func() error {
		ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
		defer cancel()
		return srv.Shutdown(ctx)
	}))
	gs.SetErrHandler(gshutdown.ErrFunc(func(err error) {
		zlog.Fatalw("shutdown: %s", err)
	}))

	gs.Start()

	if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
		zlog.Fatalw("listen: %s", err)
	}

}
